package com.intpro.demo.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.intpro.demo.DAO.VeryCode;
import com.intpro.demo.Utils.DateUtil;
import com.intpro.demo.Utils.JsonUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

@Service
public class VeryService
{

	@Autowired
	MongoTemplate mongoTemplate;

	private String getRandom()
	{
		return String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
	}

	private String send(String phone) throws Exception
	{
		String randomcode = getRandom();
		DefaultProfile profile = DefaultProfile.getProfile("cn-xx",
				"xx", "xx");
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setSysMethod(MethodType.POST);
		request.setSysDomain("dysmsapi.aliyuncs.com");
		request.setSysVersion("2017-05-25");
		request.setSysAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", "intpro");
		request.putQueryParameter("TemplateCode", "SMS_180357188");
		Map<String, String> p = new HashMap<>();
		p.put("code", randomcode);
		request.putQueryParameter("TemplateParam", JsonUtil.objectToJson(p));
		CommonResponse response;
		try
		{
			response = client.getCommonResponse(request);
			//System.out.println(response.getData());
		} catch (ServerException e)
		{
			throw new Exception();
		} catch (ClientException e)
		{
			throw new Exception();
		}
		Map<String, String> m = new HashMap<>();
		m = JsonUtil.jsonToObject(response.getData(), new TypeReference<Map<String, String>>()
		{
		});
		if (m.get("Code") == null || !m.get("Code").equals("OK"))
		{
			throw new Exception();
		}
		return randomcode;

	}

	public void createVeryCode(String phone) throws Exception
	{
		VeryCode p = findPhone(phone);
		if (p == null)
		{
			p = new VeryCode();
			p.phone = phone;
			p.code = send(phone);
			p.createTime = new Date();
			p.checked = false;
		} else if (p.checked)
		{
			throw new Exception();
		} else
		{
			p.code = send(phone);
			p.createTime = new Date();
		}

		if (p.code == null)
		{
			throw new Exception();
		}
		mongoTemplate.save(p);
	}

	public VeryCode findPhone(String phone)
	{
		Query q1 = new Query(Criteria.where("phone").is(phone));
		VeryCode v = mongoTemplate.findOne(q1, VeryCode.class);
		return v;
	}

	public boolean checkCode(String phone, String code)
	{
		Query q1 = new Query(Criteria.where("phone").is(phone));
		VeryCode v = mongoTemplate.findOne(q1, VeryCode.class);

		if (v == null)
		{
			return false;
		}
		if (!v.code.equals(code))
		{
			return false;
		}
		DateUtil.timeAddSec(60, v.createTime);
		if (!DateUtil.timeAddSec(5*60, v.createTime).after(new Date()))
		{
			return false;
		}
		v.checked = true;
		mongoTemplate.save(v);
		return true;

	}

}
